//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "math\fn_getRanDirPoint.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_center","_dir", "_min_dist","_ran_angle", "_center_x", "_center_y", "_ret_val", "_co", "_angle", "_x1", "_y1", "_helper","_slope_dist","_partial"];
DEFAULT_PARAM(_center,0,[]);
DEFAULT_PARAM(_dir,1,0);
DEFAULT_PARAM(_min_dist,2,200);
DEFAULT_PARAM(_ran_angle,3,35);
DEFAULT_PARAM(_slope_dist,4,-1);

_center_x = _center select 0;
_center_y = _center select 1;
_ret_val = [];_co = 0;
_partial = [];
while {count _ret_val == 0 && _co < 100} do {
	_angle = _dir - (_ran_angle / 2) + floor (random _ran_angle);
	_randradius = _min_dist + random(_min_dist * 0.25);
	_x1 = _center_x - (_randradius * sin _angle);
	_y1 = _center_y - (_randradius * cos _angle);
	if (!(surfaceiswater [_x1, _y1])) then {
		_helper = "Land_HelipadEmpty_F" createVehicleLocal [_x1,_y1,0];
		if (!(surfaceIsWater [getPosASL _helper select 0, getPosASL _helper select 1])) then {
			_slope = 0;
			// A negative slope dist will prevent a slope check, sometimes we don't need it.
			if (_slope_dist >= 0) then {
				_slope = [position _helper,_slope_dist] call FFUNC(getSlope);
			};
			if (_slope < 0.5) then {
				_ret_val = [getPosASL _helper select 0, getPosASL _helper select 1,0];
			} else {
				_partial = [getPosASL _helper select 0, getPosASL _helper select 1,0];
			};
		};
		deleteVehicle _helper;
	};
	if (count _ret_val == 0) then {_co = _co + 1};
};
TRACE_1("End",_ret_val);
TMFPROFILERSTOP;
_ret_val
